package com.dewod.producesales.viewmodel;

import android.content.Context;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.GsonUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.dewod.producesales.model.view.RequestSetting;
import com.dewod.producesales.utils.SharedUtil;
import com.dewod.producesales.utils.request.RequestApiUtil;
import com.dewod.producesales.utils.request.RequestBaseUtil;

public class NetSettingViewModel extends ViewModel {

    public MutableLiveData<String> ip = new MutableLiveData<>();

    public MutableLiveData<String> port = new MutableLiveData<>();

    public MutableLiveData<String> prefix = new MutableLiveData<>();

    public MutableLiveData<String> schema = new MutableLiveData<>();


    public void updateIp(String _ip){
        ip.postValue(_ip);
    }

    public void updatePort(String _port){
        port.postValue(_port);
    }

    public void updatePrefix(String _prefix){
        prefix.postValue(_prefix);
    }

    public void updateSchema(String _schema){
        schema.postValue(_schema);
    }

    // 加载数据
    public void loadSettings(Context context) {
        String json = SharedUtil.getInstance(context).readString(RequestSetting.KEY_REQUEST, GsonUtils.toJson(RequestSetting.init()));
        RequestSetting setting = GsonUtils.fromJson(json, RequestSetting.class);
        ip.setValue(setting.getIp());
        port.setValue(setting.getPort());
        prefix.setValue(setting.getPrefix());
        schema.setValue(setting.getSchema());
    }

    public void saveSettings(Context context) {
        RequestSetting setting = new RequestSetting();
        setting.setIp(ip.getValue());
        setting.setPort(port.getValue());
        setting.setPrefix(prefix.getValue());
        setting.setSchema(schema.getValue());
        SharedUtil.getInstance(context).writeString(RequestSetting.KEY_REQUEST, GsonUtils.toJson(setting));
        RequestBaseUtil.baseUrl = setting.getUrl();
    }

    public void resetToDefault(RequestSetting defaultSetting) {
        ip.setValue(defaultSetting.getIp());
        port.setValue(defaultSetting.getPort());
        prefix.setValue(defaultSetting.getPrefix());
        schema.postValue(defaultSetting.getSchema());
    }

    public String getUrl() {
        String _ip = (ObjectUtils.isEmpty(ip) || ObjectUtils.isEmpty(ip.getValue()) ) ? null : ip.getValue();
        String _port = (ObjectUtils.isEmpty(port) || ObjectUtils.isEmpty(port.getValue()) ) ? null : port.getValue();
        String _schema = (ObjectUtils.isEmpty(schema) || ObjectUtils.isEmpty(schema.getValue()) ) ? null : schema.getValue();
        if (_ip == null || _port == null || _schema == null) {
            throw new IllegalArgumentException("ip, port, and schema cannot be null");
        }

        StringBuilder result = new StringBuilder();
        result.append(_schema).append("://").append(_ip);

        // 判断是否需要添加端口号
        if (!(ObjectUtils.equals("https", _schema) && ObjectUtils.equals("443", _port)) &&
                !(ObjectUtils.equals("http", _schema) && ObjectUtils.equals("80", _port))) {
            result.append(":").append(_port);
        }
        String _prefix = (ObjectUtils.isEmpty(prefix) || ObjectUtils.isEmpty(prefix.getValue()) ) ? null : prefix.getValue();
        // 拼接前缀
        if(ObjectUtils.isNotEmpty(_prefix)){
            result.append("/").append(_prefix);
        }

        return result.toString();
    }

}
